package com.mt.mtyx.common.utils.generate.context;



import cn.hutool.json.JSONUtil;
import com.mt.mtyx.common.utils.generate.generate.code.GenerateStandard;
import com.mt.mtyx.common.utils.generate.generate.config.OtherCodeConfig;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;


/**
 * @author WMT
 * @Date 2023/5/26 17:27
 */
public class VelocityGenerate {

    public static void useVelocity(GenerateStandard generateStandard) {
        try {
            Velocity.init(properties());
            VelocityContext context = new VelocityContext();
            context.put("schema", generateStandard.getTableSchema());
            context.put("tableName", generateStandard.getTableName());
            context.put("baseName", generateStandard.getBaseName());
            context.put("tableData", generateStandard.getTableData());
            context.put("comment", generateStandard.getComment());
            context.put("date", DateTimeFormatter.ofPattern(generateStandard.getGlobal().getFormat()).format(LocalDate.now()));
            context.put("global", generateStandard.getGlobal());
            context.put("packing", generateStandard.getPacking());
            context.put("entity", generateStandard.getEntity());
            context.put("mapper", generateStandard.getMapper());
            context.put("service", generateStandard.getService());
            context.put("controller", generateStandard.getController());
            context.put("fields", generateStandard.getFields());
            context.put("fieldsName", generateStandard.getFieldsName());
            for (OtherCodeConfig otherCodeConfig : generateStandard.getOther()) {
                context.put(otherCodeConfig.getOther(), otherCodeConfig);
            }
            Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            systemClipboard.setContents(new StringSelection(JSONUtil.parse(generateStandard).toStringPretty()), null);
            Map<String, String> template = generateStandard.getTemplate();
            for (Map.Entry<String, String> entry : template.entrySet()) {
                String file = generateStandard.getFileMap().get(entry.getKey());
                if (!generateStandard.getGlobal().isOpenOverride()) { //是否不覆盖
                    if (new File(file).exists()) continue;
                }
                System.err.println(file);
                int index = file.lastIndexOf(File.separator);
                File pack = new File(file.substring(0, index));
                if (!pack.exists()) pack.mkdirs();
                try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
                    Template tmp = Velocity.getTemplate(entry.getValue());
                    tmp.merge(context, writer);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Properties properties() {
        Properties prop = new Properties();
        prop.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        prop.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        return prop;
    }

}
